package com.cj.mainmodule

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.cj.mainmodule.fragment.HomeFragment
import com.cj.mainmodule.fragment.NavigationFragment
import com.cj.mainmodule.fragment.SquareFragment
import com.cj.mainmodule.viewmodel.MainViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "junchen"
    }

    private val viewModel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.NewInstanceFactory()
        ).get(MainViewModel::class.java)
    }
    lateinit var tabLayout: TabLayout
    lateinit var viewPager2: ViewPager2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tabLayout = findViewById(R.id.tab_layout)
        viewPager2 = findViewById(R.id.view_pager2)
        //指示器的整个宽度是充满屏幕的 设置tabIndicatorFullWidth属性为false，默认为true 指示器线条的宽度和文字得宽度一致
        tabLayout.isTabIndicatorFullWidth = false
        //tabLayout.tabRippleColor //默认点击每一个tab的时候，会出现渐变的背景色
        //tabLayout.setTabRippleColorResource(R.color.transparent)
        //可以设置字体样式
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                Log.d(TAG, "onTabSelected: ${tab?.position}")
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                Log.d(TAG, "onTabUnselected: ${tab?.position}")
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                Log.d(TAG, "onTabReselected: ${tab?.position}")
            }

        })
        tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        val list = listOf("首页", "广场", "导航", "问答", "体系", "项目", "公众号", "项目分类", "工具")
        val list1 = listOf("首", "场", "导航", "问答", "体系", "项目", "公众号", "项目分类", "工具")
        for (s in list1) {
            tabLayout.addTab(tabLayout.newTab().setText(s))
        }
        var fragmentList: MutableList<Fragment> = arrayListOf(
            HomeFragment(),
            SquareFragment(), NavigationFragment()
        )
        viewPager2.adapter = ViewPagerAdapter(this, fragmentList)
        TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
            tab.text = list[position]
        }.attach()
    }

    fun click(view: View) {
        viewModel.getChapters1()
    }
}